from PyQt5.QtCore import Qt, QEvent, QRectF
from PyQt5.QtGui import QFocusEvent, QPainter, QColor, QPainterPath
from PyQt5.QtWidgets import QDialog

from utils.labelsample.ui_gen.setclassdialog_impl import Ui_Form


class SetClassDialog(QDialog, Ui_Form):
    def __init__(self, classcnt, parent=None):
        super(SetClassDialog, self).__init__(parent)
        self.setupUi(self)
        self.ok=False
        self.setAttribute(Qt.WA_TranslucentBackground)
        self.setWindowModality(Qt.WindowModal)
        self.setWindowFlag(Qt.FramelessWindowHint)
        self.spinBox.setMaximum(classcnt)
        self.pushButton.clicked.connect(self.onPushButtonClicked)

    def onPushButtonClicked(self):
        self.value = self.spinBox.value()
        self.ok=True
        self.accept()

    def changeEvent(self, event: QEvent):
        if event.type() == QEvent.ActivationChange and not self.isActiveWindow():
            self.reject()

    def paintEvent(self, *args, **kwargs):
        painter = QPainter()
        painter.begin(self)
        painter.setRenderHint(QPainter.Antialiasing)
        path = QPainterPath()
        path.addRoundedRect(QRectF(self.rect()), 10, 10)
        painter.fillPath(path, QColor("#8000000"))
        painter.end()

    def setValue(self, v):
        self.spinBox.setValue(v)
